<template>
  <div class="serial-wrap" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" title="系统编号" @newCreate="newCreate"></v-tableTitle>
      <GTable ref="table" :hasEdit="true" api="sysNumberRule/selectSysNumberRuleList" :otherHeight="0"
      :columnSource="tableConfig" :tableSettingVisible="false" @selection-change="selectData" @setdialog="setdialog">
    </GTable>
    <el-dialog :visible.sync="editModule.flag" :title="editModule.title" top="0" v-dialogDrag
      :close-on-click-modal="false" width="900px">
      <serialEditor v-if="editModule.flag" :id="editModule.activeId" :data.sync="editModule.data"></serialEditor>
      <div slot="footer" class="editor-footer">
        <el-button type="primary"
          :disabled="!this.btnPermNameList.includes('EN02080102') && !this.btnPermNameList.includes('WM03170102')"
          @click="submitEditor" v-show="submitBtnVisible">{{ $t("common.save") }}</el-button>
        <el-button
          :disabled="!this.btnPermNameList.includes('EN02080103') && !this.btnPermNameList.includes('WM03170103')"
          @click="cancel">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import serialEditor from "./model/edit.vue";

export default {
  name: "serial",
  mixins: [baseMixin],
  data() {
    return {
      loading: false,
      tableSelectData: [],
      tableSearhData: {},
      dialogFlag: false,
      editModule: {
        title: "",
        flag: false,
        activeId: 0,
        data: null
      },
      tableConfig: [
        {
          label: this.$t('config.sysCodeType'),
          code: "numberType",
          input:'select',
          optionsApi:"/codeLookup/NumberType",
        },
        {
          label: this.$t('config.sysCodeLevel'),
          code: "numberLevel",
          input:'select',
          optionsApi:"/codeLookup/NumberLevel",
        },
        {
          label: this.$t('config.appendChar'),
          code: "appendChar",
          input:'select',
          optionsApi:"/codeLookup/AppendChar",
        },
        {
          label: this.$t('config.numberPrefix'),
          code: "numberPrefix",
          input: "input",
        },
        {
          label: this.$t('config.yearCount'),
          code: "yearCount",
          input:'select',
          optionsApi:"/codeLookup/YearCount",
        },
        {
          label: this.$t('config.monthAddZero'),
          code: "monthAddZero",
          input:'select',
          optionsApi: "/codeLookup/YESNO",
        },
        {
          label: this.$t('config.dayAddZero'),
          code: "dayAddZero",
          input:'select',
          optionsApi: "/codeLookup/YESNO",
        },
        {
          label: this.$t('config.serialType'),
          code: "serialType",
          input:'select',
          optionsApi: '/codeLookup/SerialType',
        },
        {
          label: this.$t('config.serialCount'),
          code: "serialCount",
          input: "input",
        },
        {
          label: this.$t('app.remark'),
          code: "remark",
          input: "input",
        },
      ]
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.create'), certigier: (!this.btnPermNameList.includes('EN02080103') && !this.btnPermNameList.includes('WM03170101')), invalid: "newCreate" }
      ]
    },
    submitBtnVisible() {
      const systemType = this.editModule.data?.numberLevel;
      if (systemType == 11801 && this.$store.state.wms.toUpperCase().includes('WMWHSE')) {
        return false
      }
      return true
    }
  },
  created() {
  },
  methods: {
    submitEditor() {
      let params = this.editModule.data;
      if (!this.validateParams(params)) {
        return
      }
      if (params.enabledYear == 0) {
        params.enabledMonth = 0
        params.enabledDay = 0
      }
      if (this.editModule.activeId > 0) {
        params.id = this.editModule.activeId;
        this.$api.post("/sysNumberRule/updateSysNumberRule", params).then(res => {
          
            this.$message.success(this.$tj('config.sysCodeRule', 'app.upMes'))
            this.cancel()
            this.searhData()
          
        })
      } else {
        this.$api.post("/sysNumberRule/insertSysNumberRule", params).then(res => {
          
            this.$message.success(this.$tj('config.sysCodeRule', 'app.addMes'))
            this.cancel()
            this.searhData()
          
        })
      }
    },
    validateParams(params) {
      if (!params) {
        this.$message.error(this.$t('config.serialMes1'))
        return false
      }
      if (!params.numberType) {
        this.$message.error(this.$selectPlease("config.sysCodeType"))
        return false
      }
      if (!params.serialCount) {
        this.$message.error(this.$inputPlease("config.serialCount"))
        return false
      }
      if (params.enabledYear == 1) {
        if (params.serialType == 12102) {
          this.$message.error(this.$t('config.serialMes2'))
          return false
        }
        if (!params.yearCount) {
          this.$message.error(this.$selectPlease("config.yearCount"))
          return false
        }
      }
      if (params.enabledMonth == 1 && !params.monthAddZero) {
        this.$message.error(this.$selectPlease("config.monthAddZero"))
        return false
      }
      if (params.enabledDay == 1 && !params.dayAddZero) {
        this.$message.error(this.$selectPlease("config.dayAddZero"))
        return false
      }
      if (!params.serialType) {
        this.$message.error(this.$selectPlease("config.serialType"))
        return false
      }
      if (params.serialType == 12101 && (params.serialCount < 4 || params.serialCount > 12)) {
        this.$message.error(this.$t('config.serialCount') + this.$t('app.minAndMax', { min: 4, max: 12 }))
        return false
      }
      if (params.serialType == 12102 && (params.serialCount < 9 || params.serialCount > 12)) {
        this.$message.error(this.$t('config.serialCount') + this.$t('app.minAndMax', { min: 9, max: 12 }))
        return false
      }
      return true
    },
    cancel() {
      this.editModule.flag = false;
      this.editModule = {
        title: "",
        flag: false,
        activeId: 0,
        data: {}
      }
    },
    newCreate() {
      this.editModule.title = this.$t('config.addSysCodeRule');
      this.editModule.activeId = 0;
      this.editModule.flag = true;
    },


    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    async getTableData() {
      try {
        this.loading = true;
        const { pageSize, page } = this;
        // const filterParams = this.getFilterParams()
        const filterParams = {}
        let res = await this.$api.post(`/sysNumberRule/selectSysNumberRuleList`, { pageSize, pageNum: page, ...filterParams });
        
          const data = res.data.data
          this.tableData = data.list;
          this.page = data.pageNum;
          this.pageSize = data.pageSize;
          this.total = data.total;
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData(data, up) {
      this.pageNum = 1;
      this.getTableData();
    },
    getFilterParams() {
      if (this.$refs.table) {
        return this.$refs.table.getFilterParams();
      }
      return {}
    },
    //设置弹框
    setdialog(data) {
      this.editModule.title = this.$t('config.upSysCodeRule');
      this.editModule.data = data;
      this.editModule.activeId = data.id;
      this.editModule.flag = true;
    },
  },
  mounted() {
    if (this.$store.state.wms.toUpperCase().includes('WMWHSE')) {
      //仓库级
      this.permissionStatus = false;
      this.saveStatus = this.newStatus = true;
    }
    this.$store.commit("setSkip", {});
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
  },
  watch: {
    $route(data) {
      if (data.name === "sku") {
        this.dialogFlag = false;
      }
    },
    dialogFlag(data) {
      if (data === false) {
        this.$store.commit("setNewFlag", false);
        this.getTableData(1, {});
        this.$router.push({ name: "sku" });
        this.$store.commit("setSkip", {});
        this.$store.commit("setRouteData", {});
      }
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
    serialEditor
  }
};
</script>

<style scoped>

</style>
